package de.wesemann.mediahandbook.le.util;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;

import android.util.Log;
import de.wesemann.jdedun.entities.Paging;
import de.wesemann.jdedun.entities.district.districts.DistrictDistrict;
import de.wesemann.jdedun.entities.district.districts.DistrictDistrictsContainer;
import de.wesemann.jdedun.entities.district.streets.DistrictStreet;
import de.wesemann.jdedun.exceptions.PageNotFoundException;
import de.wesemann.jdedun.implementation.district.DistrictDistrictsReq;
import de.wesemann.jdedun.implementation.district.DistrictStreetsReq;

public class DistrictUtil {
	private static final String LOG_TAG = DistrictUtil.class.getSimpleName();

	private static final DistrictStreetsReq STREETS_REQ = new DistrictStreetsReq();
	private static final DistrictDistrictsReq DISTRICT_REQ = new DistrictDistrictsReq();
	private static final HashMap<String, String> SEARCH_TUPLE = new HashMap<String, String>();

	public static DistrictDistrict getDistrictByStreet(String street, String housenumber) throws PageNotFoundException {
		HashMap<String, String> searchTuple = new HashMap<String, String>();
		street = street.trim();
		street = street.replaceAll(" ", "%20");
		searchTuple.put("name", street.trim());
		searchTuple.put("housenumber", housenumber.trim());

		List<DistrictStreet> streets = STREETS_REQ.search(searchTuple);
		if (streets.size() >= 1) {
			return getDistrictById(streets.get(0).getDistrict_id());
		}
		throw new PageNotFoundException("couldn't found the district by Street:" + street + " and housnumber= "
				+ housenumber);
	}

	public static DistrictDistrict getDistrictByPostcode(String postcode) throws PageNotFoundException {
		if (postcode == null) {
			return null;
		}
		HashMap<String, String> searchTuple = new HashMap<String, String>();
		postcode = postcode.trim().replaceAll(" ", "");
		searchTuple.put("postcode", postcode);
		List<DistrictStreet> streets = STREETS_REQ.search(searchTuple);

		DistrictDistrict dist = getDistrictById(streets.get(0).getDistrict_id());
		if (dist != null) {
			return dist;
		}

		throw new PageNotFoundException("couln't load district by postcode: " + postcode);
	}

	public static int getDistrictIdByPostcode(String postcode) {
		if (postcode == null) {
			return -1;
		}
		HashMap<String, String> searchTuple = new HashMap<String, String>();
		postcode = postcode.trim().replaceAll(" ", "");
		searchTuple.put("postcode", postcode);
		List<DistrictStreet> streets = STREETS_REQ.searchWithLimit(1, searchTuple).getData();
		if (!streets.isEmpty()) {
			return streets.get(0).getDistrict_id();
		} else {
			return -1;
		}
	}

	public static DistrictDistrict getDistrictById(int id) {
		HashMap<String, String> searchTupleDistricts = new HashMap<String, String>();
		searchTupleDistricts.put("id", Integer.toString(id));
		List<DistrictDistrict> dists = DISTRICT_REQ.search(searchTupleDistricts);
		if (dists != null) {
			return dists.get(0);
		}
		return null;
	}

	public static DistrictDistrictsContainer getFirstDistricts(int n) throws FileNotFoundException {
		return DISTRICT_REQ.getFirstN(30);
	}

	public static DistrictDistrictsContainer getNextDistricts(Paging<DistrictDistrict> page)
			throws PageNotFoundException, FileNotFoundException {
		return DISTRICT_REQ.getNext(page);
	}

	public static List<DistrictDistrict> searchDistrictsByName(String districtName) throws FileNotFoundException {
		SEARCH_TUPLE.clear();
		Log.d(LOG_TAG, "looking for a branch with the name: " + districtName);
		SEARCH_TUPLE.put("name", String.valueOf(districtName));
		List<DistrictDistrict> districts = DISTRICT_REQ.search(SEARCH_TUPLE);
		Log.d(LOG_TAG, "found " + districts.size() + " branches with the name " + districtName);
		Log.d(LOG_TAG, "found " + districts.toString());
		SEARCH_TUPLE.clear();
		return districts;
	}

	public static String getPostcodeFromDistrict(int id) throws PageNotFoundException {
		SEARCH_TUPLE.clear();
		Log.d(LOG_TAG, "looking for a branch with the name: " + id);
		SEARCH_TUPLE.put("district_id", String.valueOf(id));
		List<DistrictStreet> streets = STREETS_REQ.searchWithLimit(1, SEARCH_TUPLE).getData();
		if (streets.size() > 0) {
			String postcode = streets.get(0).getPostcode();
			return postcode;
		}
		throw new PageNotFoundException("postcode with id " + id + " not found");
	}
}
